//The button tween is from youtube.com/watch?v=N-vREUGpQf4
import com.greensock.TweenLite;
import com.greensock.easing.*;
import fl.events.SliderEvent;
import flash.display.*;
//for the picture slide 
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
//for Brichnes Editor
import flash.display.MovieClip;
import flash.display.Sprite;
import fl.events.SliderEvent;
import flash.geom.ColorTransform;

stop();


TweenLite.from(title , 0.8 , {x: 1200 , alpha:0, delay:0.99});
TweenLite.from(banner , 0.5 , {y: -banner.height , alpha:0, delay:0.1});
TweenLite.from(slideTo , 0.5 , {x: -slideTo.width , alpha:0, delay:0.1});
TweenLite.from(MyGuidance , 0.5 , {x: -MyGuidance.width , alpha:0 ,delay:0.3});
TweenLite.from(SendMessage , 0.5 , {x: -SendMessage.width , alpha:0 ,delay:0.3});
TweenLite.from(AboutMe , 0.7 , {x: -AboutMe.width , alpha:0 , delay : 0.11});
TweenLite.from(Home , 0.8 , {x: -Home.width , alpha:0.15});


//###################### Styling the Buttons   #########################

var customTextFormat:TextFormat = new TextFormat();
customTextFormat.size = 20 ;
customTextFormat.color = 0x0000ff;
Home.setStyle("textFormat" , customTextFormat);
Home.setSize(120 , 26);
AboutMe.setStyle("textFormat" , customTextFormat);
AboutMe.setSize(120 , 26);
SendMessage.setStyle("textFormat" ,customTextFormat);
SendMessage.setSize(160 , 26);
MyGuidance.setStyle("textFormat" , customTextFormat);
MyGuidance.setSize(140 , 26);
slideTo.setStyle("textFormat" , customTextFormat);
slideTo.setStyle("textFormat" , customTextFormat);
slideTo.setSize(120 , 26);




//########################## Full screen   ##############################

function setFullScreen():void {
	GoFullScreen.gotoAndStop("end");
if (stage.displayState== "normal") {
stage.displayState="fullScreen";
stage.scaleMode = StageScaleMode.NO_SCALE;
} else {
stage.displayState="normal";
}
}

full.addEventListener(MouseEvent.CLICK, goFull);

function goFull(event:MouseEvent):void {
setFullScreen();
}

/// Full screen via key-board
//http://www.ilike2flash.com/2009/06/keyboard-controls-in-as3.html
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHit);
function keyHit(event:KeyboardEvent):void {
	if(event.keyCode == 17){
		setFullScreen();
	}
}

//###################    Sound control    ################################

var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
var myTransform = new SoundTransform();
var lastPosition:Number = 0;
mySound.load(new URLRequest("assets/sound1.mp3"));
myChannel = mySound.play();

volumectl.value = 3;
pause_btn.addEventListener(MouseEvent.CLICK, onClickPause);
volumectl.addEventListener(SliderEvent.CHANGE , volumeControl);
function onClickPause(e:MouseEvent):void{
lastPosition = myChannel.position;
if (pause_btn.selected == true ){
	//trace("enabled dimitrios");
	myChannel.stop();
	pauseSound.gotoAndStop(31);
	stage.removeEventListener(Event.ENTER_FRAME, moveBar);
	//soundBars.mc1.height=mc2.height=mc3.height=mc4.height=mc5.height=3;
}else {
	myChannel = mySound.play(lastPosition);
	pauseSound.gotoAndPlay("begin");
	stage.addEventListener(Event.ENTER_FRAME, moveBar);
}

}

//play_btn.addEventListener(MouseEvent.CLICK, onClickPlay);

function onClickPlay(e:MouseEvent):void{
myChannel = mySound.play(lastPosition);
}

function volumeControl(evt:SliderEvent):void {
	var adjVol:SoundTransform;
      	 adjVol = new SoundTransform(evt.target.value);
      	//sndChannel.soundTransform = adjVol ;
		myChannel.soundTransform = adjVol ;
      }
//###################  Content navigation	  ############################# 
var removedcomp:String ;
AboutMeTextArea.editable = false;
AboutMeTextArea.setStyle("backgroundColor" , 0x000000);
var homeBtnClicked:Boolean = false;

SendMessage.addEventListener(MouseEvent.CLICK , SendMessageListener);
AboutMe.addEventListener(MouseEvent.CLICK , textAreaListener);  
Home.addEventListener(MouseEvent.CLICK , homeAreaListener);
MyGuidance.addEventListener(MouseEvent.CLICK , myGuidanceListener);
var aboutmeText:String = "hello dimitiros how are you ";
AboutMeTextArea.text = aboutmeText;

function textAreaListener(evt:MouseEvent):void {
	frontText.visible = false;
	removedcomp = "AboutMeTextArea";
	checkVideoPlStatus();
TweenLite.to(MyGuidanceComp , 0.9 , {x: 1843 , alpha:1, delay:0.6});
TweenLite.to(emailForm , 0.9 , {x:-1100 ,  delay:0.2});	
TweenLite.to(frontText , 0.9 , {x:-400 ,  delay:0.1});	
TweenLite.to(AboutMeTextArea , 0.8 , {x: 200 , alpha:1, delay:1});
	
}
function checkVideoPlStatus():void{	
	if(this.videoPlayerstatus != true ){		 
	TweenLite.to(my_loader ,2,{alpha: 0});
	}else {		
my_player.pauseVideo();
Videopause_btn.alpha = 0;
//Videoplay_btn.visible = false ;
var xps = stage.stageWidth/40;
var yps:Number = stage.stageHeight/2 +stage.stageHeight/4;
TweenLite.to(backgroundPicture,2,{alpha: 1 ,delay :1.2});
TweenLite.to(my_loader , 2 ,{width : 120 , height : 120 , y:yps , x : xps});
TweenLite.to(my_loader , 2 , { alpha : 0 , overwrite:0 , delay:1});
this.videoPlayerstatus = false;		
	}
	
}

function homeAreaListener(evt:MouseEvent):void {
	trace("the framerate is   :" +stage.frameRate);
	stage.frameRate = 24;	
	this.videoBtnClicked = true;
	if (this.videoPlayerstatus == true){
my_player.pauseVideo();
Videopause_btn.alpha = 0;
Videoplay_btn.alpha = 0;
//Videoplay_btn.visible = false ;
var xps = stage.stageWidth/40;
var yps:Number = stage.stageHeight/2 +stage.stageHeight/4;
TweenLite.to(backgroundPicture,2,{alpha: 1 ,delay :1.2});
TweenLite.to(my_loader ,2,{width: 120,height:120,y:yps,x:xps});
this.videoPlayerstatus = false;		
	}else {		
	TweenLite.to(my_loader ,3,{alpha: 1 ,delay: 1.5});
	}
	
	//my_loader.visible = true;
//	TweenLite.to(my_loader , 2,{alpha:1 });
TweenLite.to(frontText , 0.9 , {x:-400 ,  delay:0.1});	
TweenLite.to(MyGuidanceComp , 0.9 , {x: 1843 , alpha:1, delay:0.2});
TweenLite.to(emailForm , 0.9 , {x:-1100 ,  delay:0.2});	
TweenLite.to(AboutMeTextArea , 0.9 , {x:-1400 ,  delay:0.9});	
	
}//end homeAreaListener
function SendMessageListener(evt:MouseEvent):void {
	stage.frameRate = 24;
	//this.videoPlayerstatus = true
	checkVideoPlStatus();
	//my_loader.visible = false;
	
	TweenLite.to(frontText , 0.9 , {x:-400 ,  delay:0.1});	
	TweenLite.to(MyGuidanceComp , 0.9 , {x: 1843 , alpha:1, delay:0.2});
	TweenLite.to(AboutMeTextArea , 0.5 , {x:-1400 ,  delay:0.9});	
	TweenLite.to(AboutMeTextArea , 0.9 , {x:-1400 ,  delay:0.9});
	TweenLite.to(emailForm , 0.9 , {x:500 ,  delay:0.6});	
}
function myGuidanceListener(evt:MouseEvent):void {
//enable the animation of the text inside of this component
stage.frameRate = 24;
checkVideoPlStatus();
MyGuidanceComp.textAnimate(true) ;
TweenLite.to(frontText , 0.9 , {x:-400 ,  delay:0.1});	
TweenLite.to(AboutMeTextArea , 0.5 , {x:-1400 ,  delay:0.2});	
TweenLite.to(emailForm , 0.5 , {x:-1100 ,  delay:0.2});
TweenLite.to(MyGuidanceComp , 0.9 , {x: 512 , alpha:1, delay:0.6});
}


//##################### Sound Graphs   ###################################
// http://www.ilike2flash.com/2010/07/sound-visualisation-effect-in.html
//Counter to hold position of the array index.
var counter:int=0;

//Array to hold all the movie clip instances. 
var mcArray:Array=[soundBars.mc1,soundBars.mc2,soundBars.mc3,soundBars.mc4,soundBars.mc5];

//Sets the starting height of the movie clips to 5 pixels.
soundBars.mc1.height=soundBars.mc2.height=soundBars.mc3.height=soundBars.mc4.height=soundBars.mc5.height=5;

//This function is used to generate random numbers.
function randomNumbers(min:Number,max:Number) {
    var Results:Number=Math.floor(Math.random()*max)+min;
    return Results;
}

stage.addEventListener(Event.ENTER_FRAME, moveBar);

//This function tweens the bars to random heights.
function moveBar(event:Event):void {   
    if(counter==mcArray.length){
        counter = 0;
    }else{
    TweenLite.to(mcArray[counter], 0.5, {height: randomNumbers(10,50)});
    counter++;
    }
}

//######################  Begin Slide ####################################
// Slideshow :http://www.republicofcode.com/tutorials/flash/as3slideshow/6.php
 
 slideTo.addEventListener(MouseEvent.CLICK , beginSlideListener1);
 Kourtina.returnHome.alpha = 0; //caution do not delete this code
 function beginSlideListener1(evt:MouseEvent):void {	 
 checkVideoPlStatus();
	Kourtina.alpha = 0 ;
	GoFullScreen.gotoAndStop("end") ; //caution do not delete this item
	stage.displayState="fullScreen";
	stage.scaleMode = StageScaleMode.NO_SCALE;
	
stage.frameRate = 35;
TweenLite.to(emailForm , 0.9 , {x:-1100 ,  delay:0.2});	
TweenLite.to(frontText , 0.9 , {x:-400 ,  delay:0.1});	
TweenLite.to(MyGuidanceComp , 0.9 , {x: -1843 ,  alpha:0, delay:0.3});
//TweenLite.to(MyGuidanceComp , 0.9 , {x: 2000 ,  alpha:1, delay:4});
TweenLite.to(AboutMeTextArea , 0.9 , {x:-1400 ,  delay:0.9});	
TweenLite.to(Kourtina , 3 , {x: 489 , alpha:1, delay:0.2});	

var myTimer:Timer = new Timer(2400,1);
myTimer.addEventListener(TimerEvent.TIMER, timerListener);
function timerListener(e:TimerEvent):void{
beginSlideListener2();
}
myTimer.start();
	
 }

function beginSlideListener2():void{
//When Kourtina is on place ,animate the button on Kourtina and continue
TweenLite.to(this.Kourtina.returnHome , 4 , {alpha:1 , delay:2});	
var my_speed:Number;
var my_total:Number;
var my_images:XMLList;

var my_loaders_array:Array=[];
var my_labels_array:Array=[];
var my_success_counter:Number=0;
var my_playback_counter:Number=0;

var my_slideshow:Sprite = new Sprite();
//caution this name"lalakis"will be used to remove this object from the display list
my_slideshow.name = "lalakis";

var my_image_slides:Sprite = new Sprite();
my_image_slides.name = "lalakis2";
var my_label_slides:Sprite = new Sprite();
my_label_slides.name = "lalakis3";
var my_preloader:TextField;
//first instantiate and than define name

var my_timer:Timer;
var my_prev_tween:Tween;
var my_tweens_array:Array=[];

var my_xml_loader:URLLoader = new URLLoader();
my_xml_loader.load(new URLRequest("slideshow.xml"));
my_xml_loader.addEventListener(Event.COMPLETE, processXML);

function processXML(e:Event):void {
	var my_xml:XML=new XML(e.target.data);
	my_speed=my_xml.@SPEED;
	my_images=my_xml.IMAGE;
	my_total=my_images.length();

	loadImages();

	my_xml_loader.removeEventListener(Event.COMPLETE, processXML);
	my_xml_loader=null;
}

function loadImages():void {
	for (var i:Number = 0; i < my_total; i++) {
		var my_url:String=my_images[i].@URL;
		var my_loader:Loader = new Loader();
		my_loader.load(new URLRequest(my_url));
		my_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
		my_loaders_array.push(my_loader);

		var my_label:TextField = new TextField();
		my_label.text=my_images[i].@TITLE;
		my_label.autoSize=TextFieldAutoSize.LEFT;
		my_labels_array.push(my_label);

	}//end for_loop

	my_preloader = new TextField();
	var format:TextFormat = new TextFormat();//made by dimitrios
            format.font = "Verdana";
            format.color = 0xFFFFFF;
            format.size = 25;
	my_preloader.name = "lalakis4";
	 my_preloader.defaultTextFormat = format
	my_preloader.text="Loading..................";
	my_preloader.autoSize=TextFieldAutoSize.CENTER;
	my_preloader.x = (stage.stageWidth - my_preloader.width)/2;
	my_preloader.y = (stage.stageHeight - my_preloader.height)/2;
addChild(my_preloader);

} //end loadImages function

function onComplete(e:Event):void {

	my_success_counter++;
	if (my_success_counter==my_total) {
		startShow();
	}

	var my_loaderInfo:LoaderInfo=LoaderInfo(e.target);
	my_loaderInfo.removeEventListener(Event.COMPLETE, onComplete);

}

function startShow():void {

	removeChild(my_preloader);
	my_preloader=null;

	Kourtina.addChildAt(my_slideshow , 1);
	Kourtina.getChildAt(1).alpha = 0;
	my_slideshow.x = -400;
	my_slideshow.y = -350; 
var obj:DisplayObject = Kourtina.getChildAt(1);
//trace("zaza"+ obj);-->object Sprite or trace("zaza"+obj.name);-->lalakis
TweenLite.to(obj , 5 , { alpha:1, delay:0.6});
	my_slideshow.addChild(my_image_slides);
	//do not reveal the label
	//my_slideshow.addChild(my_label_slides);
	
	nextImage();

	my_timer=new Timer(my_speed*1000);
	my_timer.addEventListener(TimerEvent.TIMER, timerListener);
	my_timer.start();
	


}

function nextImage():void {

	var my_image:Loader=Loader(my_loaders_array[my_playback_counter]);
	my_image_slides.addChild(my_image);
	my_image.x = (stage.stageWidth - my_image.width)/2;
	my_image.y = (stage.stageHeight - my_image.height)/2;
	my_tweens_array[0]=new Tween(my_image,"alpha",Strong.easeOut,0,1,1,true);

	var my_label:TextField=TextField(my_labels_array[my_playback_counter]);
	my_label.textColor = 0xFFFFFF;
	my_label_slides.addChild(my_label);
	my_label.x=my_image.x;
	my_label.y=my_image.y+my_image.height;
	my_tweens_array[1]=new Tween(my_label,"alpha",Strong.easeOut,0,1,1,true);

}

function timerListener(e:TimerEvent):void {

	hidePrev();

	my_playback_counter++;
	if (my_playback_counter==my_total) {
		my_playback_counter=0;
	}
	nextImage();

}

function hidePrev():void {

	var my_image:Loader=Loader(my_image_slides.getChildAt(0));
	my_prev_tween=new Tween(my_image,"alpha",Strong.easeOut,1,0,1,true);
	my_prev_tween.addEventListener(TweenEvent.MOTION_FINISH, onFadeOut);

	var my_label:TextField=TextField(my_label_slides.getChildAt(0));
	my_label.textColor = 0xFFFFFF;
	my_tweens_array[2]=new Tween(my_label,"alpha",Strong.easeOut,1,0,1,true);

}

function onFadeOut(e:TweenEvent):void {
	my_image_slides.removeChildAt(0);
	my_label_slides.removeChildAt(0);
}
 }
 
//###################     Remove kourtina 	#############################
 
 Kourtina.returnHome.addEventListener(MouseEvent.CLICK , returnHomeListener);
 function returnHomeListener(evt:MouseEvent):void {
	 
	setFullScreen()
 var obj:DisplayObject = Kourtina.getChildAt(1);
trace("zaza"+ obj);
//Animate the Alpha of the button on the kourtina	 
 TweenLite.to(this.Kourtina.returnHome , 2 , { alpha: 0 ,delay: 0.2 });
 //Animate the picture on Kourtina
 TweenLite.to(obj , 2 , { alpha: 0 , delay: 0.2 });
	 //if (this.Kourtina.contains(this.Kourtina.returnHome)) {  --ok works
	 if (this.Kourtina.contains(this.Kourtina.returnHome)){
		trace("YES");
		
	} else {
		trace("NO");
	}	
	

TweenLite.to(this.Kourtina , 4 , {x : 2100 , alpha : 0 , delay : 1 });//ok 
//this.Kourtina.removeChild(this.Kourtina.getChildByName("lalakis")); // works!!!!
this.Kourtina.my_slideshow = null;	

///just chech if object my_slideshow is removed from stage 
/// This code is not working and don't now way :)
	/*if (this.Kourtina.contains(this.Kourtina.my_slideshow)) {
		trace("YES it contains my_slideshow ");
	} else {
		trace("NO it does not contain my_slideshow");
	}*/
	
	if (this.Kourtina.my_slideshow == null) {
		trace("YES object removed from the display list");
	} else {
		trace("NO object is not removed from the display list");
	}
	
TweenLite.to(my_loader , 2 , { alpha : 1 , overwrite:0 , delay:2});
//TweenLite.to(this.Kourtina , 4 , {x : 2100 , alpha : 0 , delay : 1 });//ok
//TweenLite.to(this.Kourtina.my_slideshow , 0.2 , {alpha:0 });
//this.Kourtina.my_slideshow.alpha = 0.1;
//this.Kourtina.removeChildAt(1); ///works!!!!!!!!!!!!!
//this.Kourtina.removeChild(getChildByName("lalakis")); False

trace(this.Kourtina.getChildAt(0).name);
trace(this.Kourtina.getChildAt(1).name);
trace(this.Kourtina.getChildAt(0).name);
	
	/*var my_timer1:Timer;
	my_timer1=new Timer(100 , 1);
	my_timer1.addEventListener(TimerEvent.TIMER, removechilda);
	my_timer1.start();
	function removechilda():void {
//this.Kourtina.removeChild(this.Kourtina.getChildByName("lalakis"));
		//this.Kourtina.removeChild(this.Kourtina.returnHome);
		//this.Kourtina.returnHome.alpha = 0.2;//ok
		//this.Kourtina.removeChildAt(1); ///works!!!!!!!!!!!!!
		trace("ok removed");
	}*/
 }

 //#################  home page video ##############
 
 Security.allowDomain("www.youtube.com");
 var videoPlayerstatus:Boolean = false; //false is equal to small
 var videoPlayerApear:Boolean = true ; //true is wqual to appear on stage
var my_player:Object;
var my_loader:Loader = new Loader();
my_loader.load(new URLRequest("http://www.youtube.com/apiplayer?version=3"));
my_loader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderInit);
function onLoaderInit(e:Event):void{
addChild(my_loader);
//this works   my_loader.y = 470;
//this works   my_loader.x = -100;
my_loader.x = stage.stageWidth/20-50;
my_loader.y = stage.stageHeight/2 +stage.stageHeight/4;
//my_loader.alpha = 0.2;
//my_loader.y = 150;
//my_player.mute();
my_player = my_loader.content;
my_player.addEventListener("onReady", onPlayerReady);

}

function onPlayerReady(e:Event):void{
//my_player.setSize(550,450);
//my_player.width = 120;
//my_player.height = 120;
my_loader.width = 120;
my_loader.height = 120;
my_player.cueVideoById("ZLnp7X_8dz8",0);
my_player.addEventListener(MouseEvent.CLICK ,myPlayerClicked);
my_player.mute();
//ZLnp7X_8dz8  archeological museum
//_OBlgSz8sSM&feature=player_embedded

}
function myPlayerClicked(evt:MouseEvent):void {

my_player.playVideo();
var xpos:Number = (stage.stageWidth -my_loader.width)/6;
this.videoPlayerstatus = true;  //global see L.457 , and true means big - window 
TweenLite.to(backgroundPicture , 2 , {alpha:0.1});
TweenLite.to(my_loader ,2,{width:650,height:440,x:xpos , y:120 ,delay: 2});
TweenLite.to(Videopause_btn , 2 ,{alpha: 1 ,delay :3});
	this.videoPlayerstatus = true;
	
}

Videoplay_btn.addEventListener(MouseEvent.CLICK, playVid);
function playVid(e:MouseEvent):void {
my_player.playVideo();
this.videoPlayerstatus = true;
my_player.mute();
Videoplay_btn.visible =true;
Videopause_btn.visible = true;
Videoplay_btn.enabled = false ;
Videopause_btn.enabled= true;
}
Videopause_btn.addEventListener(MouseEvent.CLICK, pauseVid);
function pauseVid(e:MouseEvent):void {
my_player.pauseVideo();
Videoplay_btn.visible = true;
Videoplay_btn.enabled = true;
Videopause_btn.enabled = false;

}
Videopause_btn.alpha = 0;
Videoplay_btn.visible = false ;
 
 
 //###########################  Brightnes Editor ######################
 
 /*var colorTransform:ColorTransform = new ColorTransform();
		
panel.slider.addEventListener(SliderEvent.CHANGE, updateBrightness);
		
	private function updateBrightness(e:SliderEvent):void {
			colorTransform.redOffset = e.value;
			colorTransform.greenOffset = e.value;
			colorTransform.blueOffset = e.value;
			image.transform.colorTransform = colorTransform;
			panel.bValue.text = e.value;
		}*/